package com.example.flowerpcback.services;

import com.example.flowerpcback.dto.Knowledge;
import com.example.flowerpcback.dto.KnowledgeExample;
import com.example.flowerpcback.dto.Tag;
import com.example.flowerpcback.dto.TagExample;
import com.example.flowerpcback.mapper.KnowledgeMapper;
import com.example.flowerpcback.mapper.TagMapper;
import com.example.flowerpcback.req.KnowledgeReq;
import com.example.flowerpcback.req.TagReq;
import com.example.flowerpcback.resp.KnowledgeResp;
import com.example.flowerpcback.resp.PageResp;
import com.example.flowerpcback.resp.TagResp;
import com.example.flowerpcback.utils.CopyUtil;
import com.example.flowerpcback.utils.SnowFlake;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class KnowledgeService {
    private static final Logger LOG = LoggerFactory.getLogger(KnowledgeService.class);

    @Resource
    private KnowledgeMapper knowledgeMapper;

    @Resource
    private SnowFlake snowFlake;

    public KnowledgeResp save(KnowledgeReq req){

        Knowledge knowledge = new CopyUtil().copy(req, Knowledge.class);
        if(req.getId() == null){
            Long id = snowFlake.nextId();
            req.setId(id);
            knowledgeMapper.insert(knowledge);
        }else {
            knowledgeMapper.updateByPrimaryKey(knowledge);
        }

        KnowledgeResp res = new CopyUtil().copy(knowledge, KnowledgeResp.class);
        return res;

    }

    public PageResp<KnowledgeResp> list(KnowledgeReq req) {
        KnowledgeExample knowledgeExample = new KnowledgeExample();
        KnowledgeExample.Criteria criteria = knowledgeExample.createCriteria();
        if(req.getName() != null) {
            criteria.andNameLike("%" + req.getName() + "%");
        }

        PageHelper.startPage(req.getPage(),req.getSize());
        List<Knowledge> knowledgeList = knowledgeMapper.selectByExample(knowledgeExample);

        PageInfo<Knowledge> pageInfo = new PageInfo<>(knowledgeList);
        PageResp<KnowledgeResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setPages(pageInfo.getPages());

        List<KnowledgeResp> knowledgeRespList = new CopyUtil().copyList(knowledgeList,KnowledgeResp.class);

        pageResp.setList(knowledgeRespList);
        return pageResp;
    }

    public List<KnowledgeResp> all(KnowledgeReq req) {
        KnowledgeExample knowledgeExample = new KnowledgeExample();
        KnowledgeExample.Criteria criteria = knowledgeExample.createCriteria();
        if(req.getName() != null) {
            criteria.andNameLike("%" + req.getName() + "%");
        }

        List<Knowledge> knowledgeList = knowledgeMapper.selectByExample(knowledgeExample);

        List<KnowledgeResp> knowledgeRespList = new CopyUtil().copyList(knowledgeList,KnowledgeResp.class);

        return knowledgeRespList;
    }

    public void delete(KnowledgeReq req){
        knowledgeMapper.deleteByPrimaryKey(req.getId());
    }

}
